import { reqGetGoodsInfo, reqAddOrUpdateShoopCart } from "@/api";
//封装游客身份模块uuid-->生产随机字符串
import {getUUID} from '@/utils/uuid_token';
const state = {
    goodInfo: {},
    //游客临时身份证
    uuid_token:getUUID()
}
const mutations = {
    GETGOODSINFO(state, goodInfo) {
        state.goodInfo = goodInfo;
    },
}
const actions = {
    //获取产品信息的action
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGetGoodsInfo(skuId)
        if (result.code == 200) {
            commit('GETGOODSINFO', result.data);
        }
    },
    //将产品添加到购物车中 服务器不会返回数据
    async addOrUpdateShoopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrUpdateShoopCart(skuId, skuNum);
        // console.log(result);
        if (result.code == 200) {
            return "成功";
        } else return Promise.reject(new Error('失败'));
    },
}
//简化数据
const getters = {
    //路径导航
    categoryView(state) {
        //空对象的categoryView属性值是undefined 所有控制台会报错，但是不会影响
        //计算出来的属性值至少是一个空对象，这样假报错信息就不会有了
        return state.goodInfo.categoryView || {};
    },
    //简化产品信息
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    //产品售卖属性
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || [];
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}